export const post = (url, data, token) => {
    return new Promise((resolve, reject) => {
        token = token ?? uni.getStorageSync('TOKEN');
        uni.request({
            url: import.meta.env.VITE_API_URL + url,
            data: data,
            header: {
                Authorization: 'Bearer ' + token ?? null,
                'X-Enterprise-Id': uni.getStorageSync('enterpriseId'),
            },
            method: 'POST',
            success: res => {
                // 请求成功，就将成功的数据返回出去
                resolve(res.data);
                if (res.data.code !== 200) {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none',
                    });
                }
                if (res.data.code === 401) {
                    uni.navigateTo({
                        url: '/pages/index/index',
                    });
                }
            },
            fail: err => {
                reject(err);
                uni.showToast({
                    title: '请求失败',
                    icon: 'error',
                });
            },
        });
    });
};

export const get = (url, params, token) => {
    return new Promise((resolve, reject) => {
        token = token ?? uni.getStorageSync('TOKEN');
        if (params) {
            url =
                Object.keys(params).length > 0
                    ? url +
                      '?' +
                      Object.keys(params)
                          .map(key => key + '=' + params[key])
                          .join('&')
                    : url;
        }
        uni.request({
            url: import.meta.env.VITE_API_URL + url,
            data: params,
            header: {
                Authorization: 'Bearer ' + token ?? null,
                'X-Enterprise-Id': uni.getStorageSync('enterpriseId'),
            },
            method: 'GET',
            success: res => {
                // 请求成功，就将成功的数据返回出去
                resolve(res.data);
                if (res.data.code !== 200) {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none',
                    });
                }
                if (res.data.code === 401) {
                    uni.navigateTo({
                        url: '/pages/index/index',
                    });
                }
            },
            fail: err => {
                reject(err);
                uni.showToast({
                    title: '请求失败',
                    icon: 'error',
                });
            },
        });
    });
};

export const put = (url, data, token) => {
    return new Promise((resolve, reject) => {
        token = token ?? uni.getStorageSync('TOKEN');
        uni.request({
            url: import.meta.env.VITE_API_URL + url,
            data: data,
            header: {
                Authorization: 'Bearer ' + token ?? null,
                'X-Enterprise-Id': uni.getStorageSync('enterpriseId'),
            },
            method: 'PUT',
            success: res => {
                // 请求成功，就将成功的数据返回出去
                resolve(res.data);
                if (res.data.code !== 200) {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none',
                    });
                }
                if (res.data.code === 401) {
                    uni.navigateTo({
                        url: '/pages/index/index',
                    });
                }
            },
            fail: err => {
                reject(err);
                uni.showToast({
                    title: '请求失败',
                    icon: 'error',
                });
            },
        });
    });
};

export const upload = file => {
    return new Promise((resolve, reject) => {
        const token = uni.getStorageSync('TOKEN');
        uni.uploadFile({
            url: import.meta.env.VITE_API_URL + '/file/upload',
            method: 'POST',
            header: {
                Authorization: `Bearer ${token}`,
                'X-Enterprise-Id': uni.getStorageSync('enterpriseId'),
            },
            // #ifdef MP-WEIXIN || APP-PLUS
            filePath: file.tempFilePaths[0],
            // #endif
            // #ifdef H5
            file: file.tempFiles[0],
            // #endif
            name: 'file',
            success: res => {
                const data = JSON.parse(res.data);
                if (data.code === 401) {
                    uni.navigateTo({
                        url: '/pages/index/index',
                    });
                }
                if (data.code === 200) {
                    resolve(data.data);
                } else {
                    uni.showToast({
                        title: data.msg,
                        icon: 'none',
                    });
                    resolve(false);
                }
            },
            fail: err => {
                uni.showToast({
                    title: '上传失败',
                    icon: 'none',
                });
                resolve(false);
            },
        });
    });
};
